define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'adminlte', 'bootstrap-datetimepicker'], function ($, undefined, Backend, Table, Form, Adminlte, datetimepicker) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'device/key/index',
                    add_url: 'device/key/add',
                    edit_url: 'device/key/edit',
                    del_url: 'device/key/del',
                    multi_url: 'device/key/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url + '/ids/' + Config.admin.id,
                sortName: 'id',
                search: true,
                commonSearch: true,
                searchFormVisible: false,
                showToggle: false,
                showColumns: false,
                showExport: true,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'key', title: __('激活码'), operate: false},
                        {field: 'status', title: __('Status'),searchList:{'0':'未激活', '1':'已激活'}, visible: false},
                        {field: 'status_text', title: __('Status'), operate: false},
                        {
                            field: 'expire_time',
                            title: __('到期时间'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                            if(row.status == 1){
                                return '';
                            }
                            return Table.api.formatter.operate.call(this, value, row, index);
                        }}
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    if ($("td:eq(2)", this).text() == "已激活") {
                        $("input[type=checkbox]", this).prop("disabled", true);
                    }
                });
            });

        },
        add: function () {
            Controller.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            var timestamp = Date.parse(new Date()) + 7 * 86400000;
            var date = Controller.api.getMyDate(timestamp);
            $('.datetimepicker').val(date);
            Form.events.datetimepicker($("form[role=form]"));

            Controller.api.bindevent($("form[role=form]"));
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            getMyDate: function (str) {
                var oDate = new Date(str),
                    oYear = oDate.getFullYear(),
                    oMonth = oDate.getMonth() + 1,
                    oDay = oDate.getDate(),
                    oHour = oDate.getHours(),
                    oMin = oDate.getMinutes(),
                    oSen = oDate.getSeconds(),
                    oTime = oYear + '-' + Controller.api.getzf(oMonth) + '-' + Controller.api.getzf(oDay) + ' ' + Controller.api.getzf(oHour) + ':' +
                        Controller.api.getzf(oMin) + ':' + Controller.api.getzf(oSen);//最后拼接时间
                return oTime;
            },
            getzf: function (num) {
                if (parseInt(num) < 10) {
                    num = '0' + num;
                }
                return num;
            },
        }
    };
    return Controller;
});